package net.stock.controller;

import com.google.appengine.api.users.UserService;
import net.stock.utils.StockUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author naim
 */
public class CommonFilter implements Filter {
    private UserService userService;

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain)
            throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;

        if (userService.isUserLoggedIn()) {
            StockUtils.setLoggedUserEmail(request, userService.getCurrentUser().getEmail());
            StockUtils.setLogoutLinkInRequest(request, userService.createLogoutURL(StockUtils.HOME_PAGE));
        }
        
        filterChain.doFilter(req, res);
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void destroy() {
    }
}
